package com.zzty.txj1.controller;

import com.zzty.txj1.entity.Question;
import com.zzty.txj1.entity.SelfExam;
import com.zzty.txj1.entity.SelfQuestion;
import com.zzty.txj1.entity.User;
import com.zzty.txj1.service.QuestionService;
import com.zzty.txj1.service.SelfExamService;
import com.zzty.txj1.service.impl.QuestionServiceImpl;
import com.zzty.txj1.service.impl.SelfExamServiceImpl;
import com.zzty.txj1.util.SplitUtil;
import org.apache.tools.ant.taskdefs.condition.Http;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/historyQuestion")
public class HistoryQuestionController {

    @Autowired
    private SelfExamService selfExamService;
    @Autowired
    private QuestionService questionService;

    @RequestMapping(value = "/getHistoryQuestion",method = RequestMethod.POST)
    public List<SelfQuestion> getHistoryQuestion(HttpServletRequest request){
        User user=(User)request.getSession().getAttribute("user");
        List<SelfExam> selfExams=new ArrayList<>();
        SelfExam selfExam=new SelfExam();
        selfExam.setClassId(user.getClazzid());
        if(selfExamService.getSelfExamByClazzId(selfExam)!=null){
            selfExams=selfExamService.getSelfExamByClazzId(selfExam);
        }
        List<SelfQuestion> selfQuestions=new ArrayList<>();
        for(SelfExam selfExam1:selfExams){
            SelfQuestion selfQuestion=new SelfQuestion();
            String date=selfExam1.getDate();
            System.out.println("date is: "+date);
            SplitUtil split = new SplitUtil();
            List<Integer> questionIdList = new ArrayList<>();
            String content = "";
            questionIdList = split.getSplit(selfExam1.getQuestionId());

            for (int j = 0; j < questionIdList.size(); j++) {
                Question question = new Question();
                question.setId(questionIdList.get(j));
                System.out.println("2222"+questionIdList.get(j));
                content += j+1 + ":    " + questionService.getContentById(question) + "<br>";

            }
            System.out.println("1111"+content);
            selfQuestion.setQuestion(content);
            System.out.println("date2 is :"+date);
            selfQuestion.setDate(date);
            selfQuestions.add(selfQuestion);

        }
        return selfQuestions;
    }


}
